<?php ;
// The source code packaged with this file is Free Software,
// Copyright (C) 2005 by Ricardo Galli <gallir at uib dot es>.
// It's licensed under the AFFERO GENERAL PUBLIC LICENSE unless
// stated otherwise.
// You can get copies of the licenses here:
// 		http://www.affero.org/oagpl.html
// AFFERO GENERAL PUBLIC LICENSE is also included in the file
// called "COPYING".

include_once('Smarty.class.php');
$main_smarty = new Smarty;

include('config.php');
include(mnminclude.'html1.php');
include(mnminclude.'link.php');
include(mnminclude.'smartyvariables.php');
include(mnminclude.'ts.php');
include_once(mnminclude.'user.php');

if (!empty($_POST['reg_username'])) {
	regSubmit();
}
regShow();
die;

#----------------

function regParam($name) {
	global $db;
	if (!empty($_POST[$name])) {
		return $db->escape(trim($_POST[$name]));
	} else {
		return null;
	}
}

function regAssign($name, $value) {
	global $main_smarty;
	if (!empty($value)) {
		$main_smarty->assign($name, $value);
	}
}

function regSubmit() {
	$username = regParam('reg_username');
	$email = regParam('reg_email');
	$email2 = regParam('reg_email2');
	$password = regParam('reg_password');
	$password2 = regParam('reg_password2');

	regAssign('reg_username', $username);
	regAssign('reg_email', $email);

	$error = regCheck($username, $email, $email2, $password, $password2);
	if (! $error) {
		register_add_user($username, $email, $password);
	}
}

function regConf($name) {
	global $main_smarty;
	return $main_smarty->get_config_vars($name);
}

function regShow() {
	global $main_smarty;
	global $the_template;

	$vars = '';
	check_actions('register_top', $vars);

	$breadcrumb1 = regConf('PLIGG_Visual_Breadcrumb_Register');
	$navwhere['text1'] = $breadcrumb1;
	$navwhere['link1'] = getmyurl('register', '');
	$main_smarty->assign('navbar_where', $navwhere);
	$main_smarty->assign('posttitle', " / " . $breadcrumb1);

	// pagename
	define('pagename', 'register'); 
	$main_smarty->assign('pagename', pagename);

	// sidebar
	$main_smarty = do_sidebar($main_smarty);

	$vars = '';
	check_actions('register_showform', $vars);

	$main_smarty->assign('tpl_center', $the_template . '/register_center');
	$main_smarty->display($the_template . '/pligg.tpl');
}

function regCheck($username, $email, $email2, $password, $password2) {
	global $main_smarty;

	$userError = array();
	if (!isset($username) || strlen($username) < 3) {
		$userError[] = regConf('PLIGG_Visual_Register_Error_UserTooShort');
	}
	if (!preg_match('/^[a-zA-Z0-9\-]+$/', $username)) {
		$userError[] = regConf('PLIGG_Visual_Register_Error_UserInvalid');
	}
	if (user_exists(trim($username))) {
		$userError[] = regConf('PLIGG_Visual_Register_Error_UserExists');
	}
	$main_smarty->assign('form_username_error', $userError);

	$emailError = array();
	if ($email !== $email2) {
		$emailError[] = 'The email addresses do not match';
	}
	if (!check_email(trim($email))) {
		$emailError[] = regConf('PLIGG_Visual_Register_Error_BadEmail');
	}
	if (email_exists(trim($email))) {
		$emailError[] = regConf('PLIGG_Visual_Register_Error_EmailExists');
	}
	$main_smarty->assign('form_email_error', $emailError);

	$pwError = array();
	if (strlen($password) < 5) {
		$pwError[] = regConf('PLIGG_Visual_Register_Error_FiveCharPass');
	}
	if ($password !== $password2) {
		$pwError[] = regConf('PLIGG_Visual_Register_Error_NoPassMatch');
	}
	$main_smarty->assign('form_password_error', $pwError);

	$vars = array('username' => $username);
	$vars = array('email' => $email);
	check_actions('register_check_errors', $vars);

	return ((isset($vars['error']) && $vars['error'])
			|| $userError
			|| $emailError
			|| $pwError);
}

function register_add_user($username, $email, $password) {
	global $current_user;

	$user = new User();
	$user->username = $username;
	$user->pass = $password;
	$user->email = $email;
	$user->level = 'newbie';
	if ($user->Create()) {
		define('registerdetails', $username . ';' . $password . ';' . $email);
		$vars = '';
		check_actions('register_success_pre_redirect', $vars);

		$url = getmyurl('login', my_pligg_base . '/');
		$url .= (strpos($url, '?') === false) ? '?' : '&';
		$url .= 'regdone=1';
		header('Location: ' . $url);
	}
}
